/**
 * Created by Administrator on 2017/12/4.
 */
import React from 'react';
import {
    R_header
} from '../common/index'
import WxServer from '../../wx/WxServer'
import './style/business.scss'
import Popup from '../common/popup'
import success from './img/success.png'
import fail from './img/fail.png'
import imgSao from '../../img/sao.png'
import {
    HttpService,
    Tips,
    browser,
    GetDateStr,
    ApiUploadImg,
    fmtDate
} from '../../Tools'

export  default  class NumberVerify extends React.Component {
    constructor(props) {
        super(props);
        this.state={
            flag:false
        }
        this.config = {
            isSure: false,
            isCancel: false,
            tip:true,
            no: '我知道了',
            content:`<div class="tipimg"><img  src=${success} /><p>验证成功</p></div>`,
            no_cb: () => {
                this.setState({
                    flag: false
                });
            this.context.router.push('/businessIndex/2');
            }
        };
        this.verifyClick=this.verifyClick.bind(this)
    }

    static contextTypes = {
        router: React.PropTypes.object.isRequired
    };


    componentDidMount() {
        WxServer.init()
    }

    verifyClick(){
        let bcode=document.getElementsByClassName('serial')[0].value;
        if(bcode){
            this.getData(bcode)
        }else {
            Tips('请输入序列号！')
        }
    }

    getData(code){
        HttpService.query({
            url: '/jf/business/goodsArchives/consume',
            data: {
                companyId: localStorage.companyId,
                serialNumber:code,
                token: localStorage.btoken,
            },
            success: (res => {
                if (res.code == 50006) {
                    this.setState({
                        flag:true
                    })
                }else{
                    this.config.content = `<div class="tipimg"><img  src=${fail} /><p>验证失败</p><p>${res.data}</p></div>`;
                    this.setState({
                        flag:true
                    })
                    this.config.no_cb = () => {
                        this.setState({
                            flag: false
                        });
                    }
                }
            })
        })
    }

    saoInput() {
        var rec=this;
        wx.scanQRCode({
            needResult: 1,
            scanType: [
                "qrCode", "barCode"
            ],
            success: (res) => {
               const bcode=JSON.parse(res.resultStr).id;
               if(bcode){
                   rec.getData(bcode)
               }
            }
        });
    }
    render() {
        const {flag} = this.state;
        return (
            <div className="">
                <R_header title="消费验证" left="1" />
                <Popup config={this.config} blockOrNone={flag} _flag={flag}/>
                <div className="numberVerify">
                    <span id="show"></span>
                    <div className="enterInput">
                        <h4>请输入序列号：</h4>
                        <div className="form-control saoBox">
                            <input className="serial" />
                            <img className="sao" src={imgSao} onClick={this.saoInput.bind(this)}/>
                        </div>
                    </div>
                    <div className="start">
                        <button className="btn btn-primary Btn" onClick={this.verifyClick}>开始验证</button>
                    </div>
                </div>

            </div>

        )
    }
}
